package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.domain.Settings;
import com.itheima.dubbo.interfaces.SettingsApi;
import com.itheima.dubbo.mysql.mapper.SettingsMapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yaohu
 * @date 2021/12/15
 * @Version 1.0
 * @describe
 */
@Service
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 查询用户通用设置
     *
     * @param userId
     * @return
     */
    @Override
    public Settings querySettings(Long userId) {
        return settingsMapper.selectOne(Wrappers.lambdaQuery(Settings.class).eq(Settings::getUserId, userId));
    }

    /**
     * 保存/更新通用设置
     *
     * @param settings
     */
    @Override
    public void addSettings(Settings settings) {
        if (ObjectUtil.isNull(querySettings(settings.getUserId()))) {
            settingsMapper.insert(settings);
        } else {
            settingsMapper.update(settings, Wrappers.lambdaQuery(Settings.class));
        }
    }
}
